package com.shop.cereshop.app.controller.product;


import com.github.pagehelper.PageInfo;
import com.shop.cereshop.app.page.product.ProductVisitListVO;
import com.shop.cereshop.app.param.product.DeleteProductVisitParam;
import com.shop.cereshop.app.param.product.QueryProductVisitDTO;
import com.shop.cereshop.app.service.product.CereProductVisitService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/productVisit")
@Slf4j(topic = "ProductVisitController")
@Api(value = "商品浏览记录模块", tags = "商品浏览记录模块")
public class ProductVisitController {

    @Resource
    private CereProductVisitService cereProductVisitService;


    @ApiOperation(value = "查询用户浏览商品记录")
    @GetMapping("/records")
    public Result<PageInfo<ProductVisitListVO>> productVisitRecord(QueryProductVisitDTO queryDTO, HttpServletRequest request) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (user == null) {
            return new Result(CoReturnFormat.USER_NOT_LOGIN);
        }
        queryDTO.setBuyerUserId(user.getBuyerUserId());
        return cereProductVisitService.getProductVisitPage(queryDTO);
    }

    @ApiOperation(value = "清空用户浏览商品记录")
    @DeleteMapping("/clearAll")
    public Result clearAll(HttpServletRequest request) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (user == null) {
            return new Result(CoReturnFormat.USER_NOT_LOGIN);
        }
        cereProductVisitService.clearByUserId(user.getBuyerUserId());
        return Result.success();
    }

    @ApiOperation(value = "删除用户某个商品浏览记录")
    @DeleteMapping("/delete")
    public Result delete(HttpServletRequest request, @NotNull(message = "visitId不为空") @RequestParam("visitId") Long visitId) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (user == null) {
            return new Result(CoReturnFormat.USER_NOT_LOGIN);
        }
        cereProductVisitService.deleteProductVisit(user.getBuyerUserId(), visitId);
        return Result.success();
    }

    @ApiOperation(value = "批量删除用户浏览商品记录")
    @PostMapping("/deleteBatch")
    public Result deleteBatch(HttpServletRequest request, @RequestBody DeleteProductVisitParam param) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (user == null) {
            return new Result(CoReturnFormat.USER_NOT_LOGIN);
        }
        cereProductVisitService.batchDeleteProductVisit(user.getBuyerUserId(), param.getVisitIds());
        return Result.success();
    }

}
